package ch.unisi.inf.pfii.teamwhite.risk.graphic.menuFrame.dialogs;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch@lu.unisi.ch
 *
 */

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.menuFrame.MenuFrame;

public final class PlayerCreatorDialog extends JDialog{
	
	private Game game;
	
	private MenuFrame menuFrame;
	private JTextArea nameArea;
	
	private String type;
	private String name;
	
	public PlayerCreatorDialog(final Game game, final MenuFrame menuFrame) {
		super(menuFrame, "New Player", true);
		
		this.game = game;
		
		this.menuFrame = menuFrame;
		
		type = "Human";
		
		final JPanel p = new JPanel();
        p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        p.setLayout(new GridBagLayout());
        final GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        p.add(nameLabel(), gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.fill = GridBagConstraints.BOTH;
        p.add(typeLabel(), gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.gridheight = 2;
        p.add(nameTextArea(), gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        p.add(typeArea(), gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        p.add(create(), gbc);
        
        add(p);
		
		setTitle("Player Settings");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		pack();
		setLocationRelativeTo(menuFrame);
		setResizable(false);
		setVisible(true);
	}
	
	private final Component nameLabel() {
		final JLabel nameLabel = new JLabel("Name: ");
        add(nameLabel);
        return nameLabel;
	}
	
	private final Component typeLabel() {
		final JLabel nameLabel = new JLabel("Type: ");
        add(nameLabel);
        return nameLabel;
	}
	
//	This method creates a text area where you can set the name of a new player
	private final Component nameTextArea() {
		final JPanel namePanel = new JPanel();
	    nameArea = new JTextArea("Bob");
	    nameArea.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {
	    			nameArea.setText ("");
			}
		});
		nameArea.setRows(1);
		nameArea.setColumns(12);
		namePanel.add(new JScrollPane(nameArea));
		add(namePanel);
		return namePanel;
	}
	
//	This method creates a menu where you can choose if the player is an Ai or Human
	private final Component typeArea(){
		final JPanel typePanel = new JPanel();
		final JComboBox aiChoice = new JComboBox();
		aiChoice.addItem("Human");
		aiChoice.addItem("Ai");
		
		aiChoice.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent ev) {
            	if(aiChoice.getSelectedIndex() == 0){
            		type = "Human";
            	}else{
            		type = "Ai";
            	}
            }
		});
		typePanel.add(aiChoice);
		add(typePanel);
		return typePanel;
	}
	
//	This method creates the button which set the name of a new player
	private final Component create() {
		final JButton create = new JButton("Create Player");
		create.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent ev) {
            	String nameChecked  = nameArea.getText();
            	if(nameChecked.equals("Gaia")){
            		nameChecked = "Gaia*";
            	}
            	game.setPlayer(nameChecked, type);
            	name = nameChecked;
            	menuFrame.setPlayer(name, type);
            	setVisible(false);
            }
		});
		add(create);
		return create;
	}
}
